package zqz;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
        //获取TargetObject类的Class对象并且创建实例化对象
        Class<?> aClass = Class.forName("cn.zqz.TargetObject");
        TargetObject targetObject = (TargetObject) aClass.newInstance();

        //获取所有类中所有定义的方法
        Method[] methods = aClass.getDeclaredMethods();
        for (Method method :
                methods) {
            System.out.println(method.getName());
        }

        //获取指定方法并调用
        Method publicMenth = aClass.getDeclaredMethod("publicMenth", String.class);
        publicMenth.invoke(targetObject, "myself");

        //获取指定参数并对参数进行修改
        Field value = aClass.getDeclaredField("value");
        value.setAccessible(true);
        value.set(targetObject, "nice");
        //然后调用private方法
        Method privateMenth = aClass.getDeclaredMethod("privateMenth");
        privateMenth.setAccessible(true);
        privateMenth.invoke(targetObject);
    }

}
